import datetime

import shortuuid

from database import db
from ..models.additional_item import AdditionalItem
from ..models.member import Member
from ..models.shop_cart import ShopCart, CartItemAdditive, CartItem

class ShopCartService(object):

    #查询member表中是否存在这个用户
    @classmethod
    def get_member_cart(cls,member_id):
        return ShopCart.query.filter(ShopCart.member_id == member_id).first()

    @classmethod
    def mark_item_ordered(cls, item):
        '''
        将cart_item标记为已生成订单
        :param item: cart_item object
        :return: item
        '''
        item.status = CartItem.STATUS_ORDERED
        return item

    @classmethod
    def get_cart_item(cls,cart_item_id):
        return  CartItem.query.filter(CartItem.id == cart_item_id).first()

    @classmethod
    def create_member_cart(cls,member_id,**kwargs):
        kwargs.update(
            {
                'member_id' : member_id,
                'updated_at' : datetime.datetime.now(),
                'status':'created',
                'created_at': datetime.datetime.now()
             }
        )
        shop_cart = ShopCart(**kwargs)
        db.session.add(shop_cart)
        db.session.commit()
        return shop_cart
    @classmethod
    def update_cart_item_count(cls,count):
        return CartItem.update(CartItem.count==count)

    #查询购物车表中是否存在当前用户的member_id
    @classmethod
    def get_member_id(cls,member_id):
        return Member.query.filter(Member.id == member_id).first()
    @classmethod
    def get_shop_cart_id(cls,member_id):
        return ShopCart.query.filter(ShopCart.member_id == member_id).first()

    @classmethod
    def cart_item_create(cls,shop_cart_id,source,product_id,offer_id,count,**kwargs):
        kwargs.update({
            'shop_cart_id':shop_cart_id,
            'source':source,
            'product_id':product_id,
            'offer_id':offer_id,
            'count':count
        })
        cart_item = CartItem(**kwargs)
        db.session.add(cart_item)
        db.session.commit()
        return db
    @classmethod
    def cart_item_update(cls,count,shop_cart_id):
       db.session.query(CartItem).filter(CartItem.shop_cart_id==shop_cart_id).update(
            {
                CartItem.count:count
            }
        )
       db.session.commit()
       return db

    @classmethod
    def get_cart_item_source(cls,items_from):
        return CartItem.query.filter(CartItem.source == items_from).all()

    @classmethod
    def get_service_by_id(cls,service_id):
        return AdditionalItem.query.filter(AdditionalItem.id == service_id).first()

    @classmethod
    def update_shop_cart_item(cls,product_id,offer_id,shop_cart_id,source,**kwargs):
        kwargs.update({
            'product_id':product_id,
            'offer_id':offer_id,
            'shop_cart_id':shop_cart_id,
            'source':source,
            'id':shortuuid.uuid()
        })
        cart_item = CartItem(**kwargs)
        db.session.add(cart_item)
        db.session.commit()
        return cart_item

    # @classmethod
    # def get_by_member(cls, member):
    #     return cls.get_or_none(
    #         (cls.member == member) &
    #         (cls.status == 'created')
    #     )
    @classmethod
    def get_item_from(cls, source, product, offer_id):
        return db.session.query(CartItem).filter(CartItem.product_id == product).filter(CartItem.source == source)\
                                     .filter(CartItem.offer_id == offer_id).first()
    @classmethod
    def cart_item_additive_create(cls,type,service_id,cart_item,title,required,**kwargs):
        kwargs.update({
            'type':type,
            'service_id':service_id,
            'cart_item':cart_item,
            'title':title,
            'required':required
        })
        cart_item_additive = CartItemAdditive(**kwargs)
        db.session.add(cart_item_additive)
        db.session.commit()
        return db